# -*- coding: utf-8 -*-
#  F7Help帮助制作类
# @Time    ： 2022/1/15  9:16
# @Auther  ： 徐良
# File     :  makeF7helpPara.py

class MakeF7helpPara(object):
    """
    产生f7帮助的参数
    """
    def __init__(self, pDict):
        '''
        功能： 1 生成F7面板需要的参数字典，2  帮助F7面板解析参数
        :param pDict: 参数字典
        1 work   是formBillWork的对象
        2 className 面板样式，f7lineEdit的类，有3种，表头，表体，属性面板
        3 obj
        '''
        if 'work' in pDict:
            self.work = pDict['work']
        else:
            self.work = None

        if 'f7dict' in pDict:
            self.f7dict = pDict['f7dict']
        else:
            self.f7dict = None

        # obj 必须要加，否则帮助面板会无法显示，可能是因为代理中editor失去焦点后释放内存的原因
        self.__paraMap = {'className': pDict['className'], 'obj': pDict['obj'], 'work': self.work}

        if 'index' in pDict:
            self.__paraMap['index'] = pDict['index']

        self.className = pDict['panelStyle']

        if self.className == 'Grid':
            pass
            # self.tableViewColumn = dic['gridCol']
            # self.__doTableView()

        elif self.pDict == 'F7HeadLineEdit':        # 表体的F7lineEdit
            self.objectName = pDict['objectName']
            # 获取字段名的序号
            column = self.work.getFormNumByFieldName(self.objectName)
            self.__doForm(column)

        elif self.panelStyle == 'Arr':
            # 获取字段名的序号
            row = dic['arrRow']
            self.__doArr(row)


    # 处理类型panelStyle='Grid'
    def __doTableView(self):
        self.__dic = self.work.bodyFieldInfo(self.tableViewColumn)  # 获取该字段相关信息
        self.__getRelationTableAbout()
        self.__getRelationFieldNameAbout()
        self.__getRTableDescriptionAbout()
        self.__getRelationHideColumnAbout()
        self.__getRelationGetAndRet()
        self.__getRelationFields()

    # 处理类型2 form表单(edit)字段
    def __doForm(self, column):
        self.__dic = self.work.headFieldInfo(column)  # 获取该字段相关信息
        self.__getRelationTableAbout()
        self.__getRelationFieldNameAbout()
        self.__getRTableDescriptionAbout()
        self.__getRelationHideColumnAbout()
        self.__getRelationGetAndRet()
        self.__getRelationFields()

    def __doArr(self, column):
        self.__dic = self.work.bodyFieldInfo(column)  # 获取该字段相关信息
        self.__getRelationTableAbout()
        self.__getRelationFieldNameAbout()
        self.__getRTableDescriptionAbout()
        self.__getRelationHideColumnAbout()
        self.__getRelationGetAndRet()
        self.__getRelationFields()

    def __doEasyGridEdt(self):  # 有单表定义的， FF7Str字典，字典字符串

        self.__paraMap["simpleTable"] = self.f7dict['simpleTable']
        self.__paraMap["detailedTable"] = self.f7dict['detailedTable']
        self.__paraMap["treeSql"] = self.f7dict['treeSql']
        self.__paraMap["simpleSql"] = self.f7dict['simpleSql']
        self.__paraMap["detailedSql"] = self.f7dict['detailedSql']

        self.__paraMap["simpleColumnList"] = self.f7dict['simpleColumnList']
        self.__paraMap["detailedColumnList"] = self.f7dict['detailedColumnList']

        self.__paraMap['windowTitle'] = self.f7dict['windowTitle']
        self.__paraMap['browseDiscription'] = self.f7dict['browseDiscription']
        self.__paraMap['searchDiscription'] = self.f7dict['searchDiscription']

        self.__paraMap['simpleFields'] = self.f7dict['simpleFields']
        self.__paraMap['detailedFields'] = self.f7dict['detailedFields']

        self.__paraMap['getFieldList'] = self.f7dict['getFieldList']
        self.__paraMap['fieldTypeList'] = self.f7dict['fieldTypeList']
        self.__paraMap['retFieldList'] = self.f7dict['retFieldList']

        self.__paraMap['simpleHideList'] = self.f7dict['simpleHideList']
        self.__paraMap['detailedHideList'] = self.f7dict['detailedHideList']

    def __getRelationTableAbout(self):
        """
        获取关于F7的FRelationTable字段的信息，分五段：
        1，浏览面板的表名
        2，搜索面板的表名
        3，浏览面板左树的sql语句
        4，浏览面板simpleTableView的model的sql语句
        5，搜索面板的detailedTableView的model的sql语句
        :return:
        """
        relationTable = self.__dic['relationTable']
        self.relationTableList = self.__getListFormStr(relationTable)
        simpleTable = self.relationTableList[0].strip()  # 浏览面板网格表表名
        detailedTable = self.relationTableList[1].strip()  # 搜索面板网格表名
        treeSql = self.relationTableList[2].strip()  # 浏览面板左树sql
        simpleSql = self.relationTableList[3].strip()  # 浏览面板simpleTableView1表sql
        detailedSql = self.relationTableList[4].strip()  # 搜索面板detailedTableView表sql

        self.__paraMap["simpleTable"] = simpleTable
        self.__paraMap["detailedTable"] = detailedTable
        self.__paraMap['treeSql'] = treeSql
        self.__paraMap['simpleSql'] = simpleSql
        self.__paraMap['detailedSql'] = detailedSql

    def __getRelationFieldNameAbout(self):
        """
        获取关于F7的FRelationFieldName的信息，用于F7浏览和搜索面板的网格中文列名，分两段
        1，用于浏览界面simpleTableView 的列名
        2，用于搜索界面detailedTableView1的列名
        :return:
        """
        relationFieldName = self.__dic['relationFieldName']
        ll = self.__getListFormStr(relationFieldName)
        simpleColumnList = ll[0].split(',')
        detailedColumnList = ll[1].split(',')
        self.__paraMap["simpleColumnList"] = simpleColumnList
        self.__paraMap["detailedColumnList"] = detailedColumnList

    def __getRTableDescriptionAbout(self):
        """
        获取关于F7的RTableDescription的信息，用于描述F7面板的Label
        1 窗口标题
        2 浏览面板的记录
        3 搜索面板的记录
        :return:
        """
        rTableDescription = self.__dic['rTableDescription']
        l = self.__getListFormStr(rTableDescription)

        self.__paraMap['windowTitle'] = l[0].strip()
        self.__paraMap['browseDiscription'] = l[1].strip()
        self.__paraMap['searchDiscription'] = l[2].strip()

    def __getRelationHideColumnAbout(self):
        """
        获取关于F7的RelationHideColumn的信息,用于隐藏F7面板的column
        1 浏览面板的隐藏列
        2 搜索面板的隐藏列
        :return:
        """
        relationHideColumn = self.__dic['relationHideColumn']
        l = self.__getListFormStr(relationHideColumn)
        self.__paraMap['simpleHideList'] = l[0].strip()
        self.__paraMap['detailedHideList'] = l[1].strip()

    def __getRelationGetAndRet(self):
        """
        获取关于F7的RelationGetAndRet的信息,用于F7面板的取值和返回
        1 从面板中取值的字段名，列表
        2 字段的数据类型，列表
        3 返回的须填写的字段名，列表，与1，一一对应，字段名不一定相同
        :return:
        """
        relationGetAndRet = self.__dic['relationGetAndRet']
        l = self.__getListFormStr(relationGetAndRet)

        self.__paraMap['getFieldList'] = l[0].strip().lower().split(',')
        self.__paraMap['fieldTypeList'] = l[1].strip().lower().split(',')
        self.__paraMap['retFieldList'] = l[2].strip().lower().split(',')

    def __getRelationFields(self):
        """
        获取关于F7的RelationFields的信息,用于F7浏览和搜索面板的动态拼SQl
        1 浏览面板字段 列表
        2 搜索面板，列表
        :return:
        """
        relationFields = self.__dic['relationFields']
        lList = self.__getListFormStr(relationFields)
        if len(lList) > 1:
            self.__paraMap['simpleFields'] = lList[0].strip()
            self.__paraMap['detailedFields'] = lList[1].strip()


    # F7参数字典
    @property
    def paraMap(self):
        return self.__paraMap

    def __getListFormStr(self, ss):
        s = ss.replace('#', '').replace('\n', '').replace('*', '').strip()  # 清除#
        ret = s.split('|')
        return ret

    def __getStr(self, ss):
        return ss.replace('#', '').replace('\n', '').replace('*', '').strip()       # 清除
